#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_test.py
@Time    :   2024/09/05 13:31:28
@Author  :   Yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
import Set_up
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable
from commDriver.modbus_plc import *

from sqlalchemy import false
sys.dont_write_bytecode = True

class Test_ReadDTCInfomation_phy(object):
    @allure.story('Test ReportDTCSnapshotIdentification Test')
    @allure.severity('normal')
    @allure.description('Test ReportDTCSnapshotIdentification with no fault in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReportDTCSnapshotIdentification_No_Fault_Default_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)


    @allure.severity('normal')
    @allure.description('Test ReportDTCSnapshotIdentification with no fault in Extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReportDTCSnapshotIdentification_No_Fault_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)

    @pytest.mark.smoke
    @allure.severity('normal')
    @allure.description('Test reportNumberOfDTCByStatusMask of 0x19 when faults occur in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_reportNumberOfDTCByStatusMask_Have_Fault_Default_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)
        self.reportNumberOfDTCByStatusMask_Have_Fault(Driver, iStep)

    @pytest.mark.smoke
    @allure.severity('normal')
    @allure.description('Test reportNumberOfDTCByStatusMask of 0x19 when faults occur in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_reportNumberOfDTCByStatusMask_Have_Fault_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)
        time.sleep(1)
        self.reportNumberOfDTCByStatusMask_Have_Fault(Driver, iStep)


    
    def reportNumberOfDTCByStatusMask_Have_Fault(self,Driver, iStep):
        time.sleep(6)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)

        with allure.step(f"Step{iStep}:Create fault of message 0x1B1 lost"):
            Driver['CAN'].set_0x1B1_lost_error(True)
            iStep += 1
            time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)
        list = Driver['CAN'].split_list_by_size(uds_resp[2:], 4)
        if [0xF2, 0x48, 0x87, 0x01] in list:
            findStatus = True
            logging.info("find 0xF2 0x48 0x87 0x01 in list")
        else:
            findStatus = False
        with assume: assert findStatus == True 
        

        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x02, 0x0B], [0x59, 0x02], 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)
        list = Driver['CAN'].split_list_by_size(uds_resp[2:], 4)
        if ([0xF2, 0x48, 0x87, 0x01] in list) and ([0x98, 0x7A, 0x17, 0x01] in list):
            findStatus = True
        else:
            findStatus = False
        with assume: assert findStatus == True


        # 电压异常时不报busoff code
        iStep = Driver['CAN'].V_ConnectCanH_CanL(True, iStep)
        iStep = Driver['CAN'].V_ConnectCanH_CanL(False, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)
        list = Driver['CAN'].split_list_by_size(uds_resp[2:], 4)
        if ([0xF2, 0x48, 0x87, 0x01] in list) and ([0x98, 0x7A, 0x17, 0x01] in list):
            findStatus = True
        else:
            findStatus = False
        with assume: assert findStatus == True
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)
        list = Driver['CAN'].split_list_by_size(uds_resp[2:], 4)
        if ([0xF2, 0x48, 0x87, 0x01] in list) and ([0x98, 0x7A, 0x17, 0x01] in list):
            findStatus = True
        else:
            findStatus = False
        with assume: assert findStatus == True
        iStep = Driver['CAN'].SetVolt(Driver, 7.5, 1.5, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19, 0x03], [0x59, 0x03], 0, iStep, False)
        list = Driver['CAN'].split_list_by_size(uds_resp[2:], 4)
        if ([0xF2, 0x48, 0x87, 0x01] in list) and ([0x98, 0x7A, 0x17, 0x01] in list) and ([0x98, 0x7A, 0x16, 0x01] in list):
            findStatus = True
        else:
            findStatus = False
        with assume: assert findStatus == True
        with allure.step(f"Step{iStep}:Remove fault of message 0x1B1 lost"):
            Driver['CAN'].set_0x1B1_lost_error(False)
            iStep += 1

        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)
